package com.app.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.app.entity.*;
import com.app.service.*;
import com.app.utils.MD5;
import com.app.utils.MainUtils;

@Controller
@RequestMapping("/backendui")
public class AdminController {
    private final static Log log = LogFactory.getLog(AdminController.class);

    @Resource
    UsersService userService;


    @RequestMapping("/index")
    public String index(HttpSession session) {
        if (session.getAttribute("user") == null) {
            return "redirect:login";
        }

        return "backend/index";
    }

    @RequestMapping("/login")
    public String login() {
        return "backend/login";
    }

    @RequestMapping("/signup")
    public String signup() {
        return "backend/reg";
    }

    @RequestMapping("/logout")
    public String logout(String retUrl, HttpSession session) {
        session.invalidate();
        return "redirect:" + retUrl;
    }
    @Resource
    InfocateService infocateService;
    @RequestMapping(value = "/signin", method = RequestMethod.POST)
    public String signin(@RequestParam("username") String username,
                         @RequestParam("password") String password, Model model,
                         HttpSession session) {
        log.info("username:{"+ username + "} login");
        Users user = userService.userLogin(username, MD5.MD51(password));

        model.addAttribute("inlogin", true);
        if (user != null) {
        	 Infocate infocate = infocateService.getInfocateById(Integer.parseInt(user.getCompany()));
            model.addAttribute("user", user);
            model.addAttribute("dept", infocate);
            session.setAttribute("dept", infocate);
            session.setAttribute("user", user);
            session.setAttribute("userid", user.getId());
            session.setAttribute("username", user.getUsername());
            session.setAttribute("usertype", user.getType());

            Users us = userService.getUsersById(Integer.parseInt(session
                    .getAttribute("userid") + ""));
            userService.update(us);
        }
        return "backend/login";
    }

    @RequestMapping(value = "/reg", method = RequestMethod.POST)
    public String reg(@RequestParam("username") String username,@RequestParam("type") String type,
                      @RequestParam("password") String password,
                      String email,
                      String tel,
                      Model model,
                      HttpSession session) {
        Users user = new Users();
        user.setEcard(MainUtils.getEcard());
        user.setMoney("100.00");
        user.setType(type);
        user.setUsername(username);
        user.setPassword(MD5.MD51(password));
        user.setEmail(email);
        user.setTel(tel);

        user.setCreated(MainUtils.getTime());
        userService.insert(user);
        model.addAttribute("state", 1);
        return "backend/reg";
    }
}
